#include <bits/stdc++.h>
using namespace std;

int get_path(vector<vector<int>>& squares_mat, 
              int x1, int y1, int x2, int y2, int path, int value){
    if(x1<0 || x1>=squares_mat.size() || y1<0 || y1>=squares_mat[0].size()) return -1;
    if(squares_mat[x1][y1] < value){
        return -1;
    }
    if(x1 == x2  && y1 == y2) return 0;

    int top=-1, bottom=-1, left=-1, right=-1;
    if(path != 1) top = get_path(squares_mat, x1-1, y1, x2, y2, 0, squares_mat[x1][y1]);
    if(path != 0) bottom = get_path(squares_mat, x1+1, y1, x2, y2, 1, squares_mat[x1][y1]);
    if(path != 3) left = get_path(squares_mat, x1, y1-1, x2, y2, 2, squares_mat[x1][y1]);
    if(path != 2) right = get_path(squares_mat, x1, y1+1, x2, y2, 3, squares_mat[x1][y1]);

    int temp = INT_MAX;
    if(top == -1 && bottom == -1 && left == -1 && right == -1) return -1;
    else{
        if(top != -1) temp = min(temp, top);
        if(bottom != -1) temp = min(temp, bottom);
        if(left != -1) temp = min(temp, left);
        if(right != -1) temp = min(temp, right);
    }
    return temp+1;
    
}


int main(int argc, char* argv[]){
    int n, m;
    cin>>n>>m;

    int x1, y1, x2, y2;
    cin>>x1>>y1>>x2>>y2;

    vector<vector<int>> squares_mat(n, vector<int>(m));
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin>>squares_mat[i][j];
        }
    }

    int path_len=0;
    path_len = get_path(squares_mat, x1-1, y1-1, x2-1, y2-1, -1, 0);

    cout << path_len << endl;

    return 0;
}